Repository navigation
OFB and CTR: verified final functions on every target; delete Rust's partial-block code - #4451
Merged
Merged
Conversation
…ED, SM4 and Triple DES (WIP: crates to come) vg_<cipher>_ofb_final: the generic fbFinal with OFB's step, as #4394's CFB finals, whose tail leaves O_n in the IV; the target-independent OFB lemmas in Proof/Modes/FbFinal.lean and CfbFinalScratch.lean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…EED, SM4 and Triple DES (WIP: crates to come) As on x86-64: the generic fbFinal with OFB's step. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
… SM4 and Triple DES (WIP: crates to come) x86's seqFinal with OFB's mode; its proof (ofbFinal_wp) in Proof/Modes/X86/SeqFinal.lean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…D, SM4 and Triple DES (WIP: crates to come) Arm's seqFinal with seq's OFB mode; its final-segment obligation (ofb_finalOk) and postcondition in Proof/Modes/Arm/SeqFinal.lean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…EED, SM4 and Triple DES (WIP: crates to come) PPC64LE's seqFinal with OFB's mode; its proof (ofbFinal_wp) in Proof/Modes/PPC64LE/SeqFinal.lean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…ED, SM4 and Triple DES (WIP: crates to come) RISC-V's seqFinal with OFB's mode; its proof (ofbFinal_wp) in Proof/Modes/RISCV64/SeqFinal.lean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…P: needs the regenerated crates) Whole blocks stay with the whole-block functions; only the bytes after them go to vg_<cipher>_ofb_final. The Rust code that ran OFB on the padded last bytes, and its comment, are deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…tes to come)
vg_aes_ofb_final: AES-CFB's final caller around the whole-block AES-OFB
function, whose tail (the block function on the IV) is OFB's.
vg_<cipher>_ctr_final: a call of the whole-block CTR function on the whole
blocks, then one on a zeroed block in a wiped stack frame, whose first
bytes are XORed into the tail; the counter is left at T_{n+1}. AES's for
every variant; target-independent lemmas in Proof/Modes/CtrFinal.lean,
Proof/AesOfb/Final.lean and Proof/AesCtr/Final.lean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…eds the regenerated crates) Whole blocks stay with the whole-block functions; only the bytes after them go to vg_aes_ofb_final, vg_aes_ctr_final, vg_camellia_ctr_final or vg_sm4_ctr_final (AES's of the variant chosen, whose features select now also requires). The Rust code that ran OFB or CTR on the padded last bytes is deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
For every x86 block-function variant, as on x86-64. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
… crates to come) As on x86-64; AES's for every variant. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…rates to come) As on the other targets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
As on the other targets: a call of the framed whole-block CTR function on the whole blocks, then one on a zeroed block in the wiped save area. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
… crates to come) As on the other targets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…crates to come) As on the other targets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
ofbFinal_of (and RISC-V's ofbFinal_out) move into the target-independent
Proof/Modes/FbFinal.lean; the CTR finals' entry-memory locality lemmas
(Camellia's ctrFinalPre_local and ctrFinalPost_local, SM4's
ctrFinalPost_local) into Proof/{Camellia,Sm4}/CtrFinal.lean. No statement,
code or registration changes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
Also renames the x86-64 Camellia CTR proof's cipher_length to ctr_cipher_length, which clashed with the OFB/CFB final proof's in the same namespace once the emitter imported both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Nov 7, 2026
#4428 emits a dispatcher for every function's implementations; the final functions are new implementations of the OFB and CTR modules. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #4139 (finding 4: CFB's partial last segment is XORed in Rust, here for OFB and CTR)
This PR gives the OFB and CTR final functions specified in #4334 verified implementations on all six targets (x86-64, AArch64, x86, Arm, PPC64LE, RISC-V):
vg_<cipher>_ofb_finalfor all eight ciphers: AES, Blowfish, Camellia, CAST5, IDEA, SEED, SM4 and Triple DES.vg_<cipher>_ctr_finalfor AES, Camellia and SM4.AES's final functions cover every variant (
_aesni,_vaes,_aes), on x86-64, x86 and AArch64.apply_keystream_finalstill gives a message's whole blocks to the whole-block functions. If a partial last block remains, it now goes to the final function, which continues from the IV or counter the whole blocks left. This is what #4394 and #4430 did for CFB. The Rust code that ran OFB or CTR over the last bytes padded to a block is deleted, along with its misleading comment ("The next output block, as OFB of a zero block, XORed into the last bytes"). Rust now only splits the buffer at a block boundary. With this PR, no mode's partial last block is computed in Rust.Design
OFB of the seven non-AES ciphers. This uses CFB: verified final functions for the non-AES ciphers; delete Rust's partial-segment XOR #4394's generic final modes with OFB's step:
fbFinalon x86-64 and AArch64;seqFinalon x86, Arm, PPC64LE and RISC-V.The loop's partial last segment leaves
Oₙ = CIPH(Oₙ₋₁)in the IV, asSpec.Ofb.CryptFinalrequires. The target-independent lemmas areofbFinal_out/ofbFinal_of/ofbFinal_of_bytes(Proof/Modes/FbFinal.lean) and the scratch/locality lemmas (Proof/Modes/CfbFinalScratch.lean). Each target adds an OFB section to itsProof/Modes/<T>/SeqFinal.leanorFbFinal.lean, and each cipher adds an OFB section next to its CFB final inProof/<C>/<T>/FbFinal*.lean. Only on Arm did the OFB proof need an extra fact: every output of the cipher is a block long, for every input.OFB of AES. This is AES-CFB: verified final functions on every target; delete Rust's partial-segment XOR #4430's AES-CFB final caller, unchanged, around the whole-block AES-OFB function of the same variant. That caller's tail calls the block function on the IV in place, then XORs the bytes; that is exactly OFB's last step. The constant-time lemmas of
Proof/AesCfb/<T>/Final.leanare generalized over theMode; their statements are otherwise unchanged.Proof/AesOfb/Final.leanis target-independent.CTR (AES, Camellia, SM4). The final function first calls the whole-block CTR function, of the same variant, on the whole blocks. If bytes are left:
Oₙ = CIPH(Tₙ)in the block and the counter atTₙ₊₁, asCtr.next … ⌈len/16⌉says;The save area, keystream included, is in a stack frame zeroed on return. The function relies only on the whole-block function's contract and constant-time proof. For Camellia and SM4, the callee is the registered stack-framed function. Target-independent pieces:
Proof/Modes/CtrFinal.lean, generic in the cipher;Proof/AesCtr/Final.lean;Proof/{Camellia,Sm4}/CtrFinal.lean, which holds the contract plumbing and the locality lemmas the frames need.AES-CTR's constant time is proven with the whole-block contract's leak: the counter's last 32 bits agree at both calls.
Each final function calls exactly the code and symbol its whole-block function's registration emits; on Arm and RISC-V, AES-OFB's tail calls the registered
vg_aes_encrypt_blocks. Constant time is checked by the taint analysis, plus the callees' own constant-time proofs for the calls. Only pointers,roundsandlenaffect timing.No TCB or Spec changes.
Tests
The existing tests check published vectors through
apply_keystream_final, whole, truncated at every length and split at every point:tests/cavp/*_ofb.rs);tests/cryptography_cast5,tests/draft_ribose_sm4);tests/rfc3686,tests/rfc5528), including RFC 3686's counter-carry case.Every length that is not a multiple of the block size now ends in a verified final function. No vectors are typed in.
I ran these locally:
VG_CPU_FEATURESset tonone,aes,ssse3andaes,ssse3,avx,avx2,vaes;VG_CPU_FEATURES=none.All of them pass. So does the whole host test suite. The following also pass:
lake buildandEmit.lean;check_lean_public_imports.pyon the changed modules, run per target, sincelake shakeon all of them at once hits a pre-existing clash between twoEcbmodules of main;cargo fmt, andclippyon all six targets.Benchmarks
None were run. The OFB and CTR benchmarks' sizes (64, 1024 and 16384 bytes) are whole blocks, so for them
apply_keystream_finalcalls the same whole-block functions as on main and nothing else. The change cannot affect their speed, and the benchmarking skill says not to run a benchmark for such a change. A partial last block, which no benchmark measures, used to cost a call of the whole-block function on a padded block. It now costs a call of the final function.Generated by Claude Code